﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MusicManagerDAO
{
    public class MusicManagerDbContext : DbContext
    {
        public MusicManagerDbContext()
            : base("Name=MusicManagerDB")
        {
        }

        public DbSet<User> Users { get; set; }
        public DbSet<Music> Music { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<User>().HasKey<int>(s => s.Id);
            modelBuilder.Entity<Music>().HasKey<int>(s => s.Id);

            //modelBuilder.Configurations.Add(new UserMap());
            //modelBuilder.Configurations.Add(new MusicMap());
        }
    }

    public class User
    {
        [Key] //identity(1,1) bei heinzelreiter...
        public int Id { get; set; }
        public string Name { get; set; }
        public string Password { get; set; }
    }

    public class Music
    {
        public int Id { get; set; }
        public int UserId { get; set; }
        public string Name { get; set; }
        public byte[] Song { get; set; }
    }

    // advanced OR mapping with the EF fluent API
    public class UserMap : EntityTypeConfiguration<User>
    {
        public UserMap()
        {
            // table name
            this.ToTable("User");
            
            // primary key
            this.HasKey<int>(t => t.Id);
        }
    }

    public class MusicMap : EntityTypeConfiguration<Music>
    {
        public MusicMap()
        {
            // table name
            this.ToTable("Music");

            // primary key
            this.HasKey(t => t.Id);
        }
    }

    internal static class DomainDtoMapping
    {
        public static DomainModel.User ToDomain(this User u)
        {
            if (u == null) return null;
            return new DomainModel.User { Id = u.Id, Name = u.Name, Password = u.Password };
        }

        public static DomainModel.Music ToDomain(this Music m)
        {
            if (m == null) return null;
            return new DomainModel.Music
            {
                Id = m.Id,
                UserId = m.UserId,
                Name = m.Name,
                Song = m.Song
            };
        }

        public static User ToEF(this DomainModel.User u)
        {
            if (u == null) return null;
            return new User { Id = u.Id, Name = u.Name, Password = u.Password };
        }

        public static Music ToEF(this DomainModel.Music m)
        {
            if (m == null) return null;
            return new Music
            {
                Id = m.Id,
                UserId = m.UserId,
                Name = m.Name,
                Song = m.Song
            };
        }
    }



}
